<template>
  <div>
    <div class="search_form">
			<div>
				<el-date-picker
					size="mini"
					v-model="searchForm.workDate"
					type="date"
					format="yyyy年MM月dd日"
					value-format="yyyy-MM-dd"
					placeholder="选择日期"
					class="others"
          clearable
				></el-date-picker>
        <el-select
					size="mini"
					clearable
					placeholder="请选择班次"
					v-model="searchForm.workTime"
					class="others"
					popper-class="selectClass"
				>
					<el-option v-for="(item, index) in workTimeList" :key="index" :label="item.dictLabel" :value="item.dictValue"></el-option>
				</el-select>
				<el-button @click="searchData" class="search_btn" type="primary" icon="el-icon-search" size="mini">
					查询
				</el-button>
			</div>
      <div>
        <el-button type="primary" v-hasPermi="['yard:DispatchWorker:insertStorehouse']" @click="showYard" size="mini">场地安排</el-button>
        <el-button type="primary" v-hasPermi="['yard:DispatchWorker:insertTallyClerk']" @click="showSteaf" size="mini">理货员派工</el-button>
        <el-button type="primary" v-hasPermi="['yard:DispatchWorker:insertRuler']" @click="showResponsibility" size="mini">检尺派工</el-button>
        <el-button type="primary" v-hasPermi="['yard:DispatchWorker:insertForeignTallyCompany']" @click="showExternalReason" size="mini">派外理单位</el-button>
      </div>
		</div>
    <el-tabs v-model="activeName" type="card" @tab-click="handleClick(activeName)">
      <el-tab-pane label="船舶计划" name="first">
        <BaseTable 
          ref="oddJobApply"
          :loading="loading"
          :data="tableData"
          :columns="columns"
          @details="handlerClick" 
          :tableHeight="tableHeight" 
          :pagination="false"></BaseTable>
      </el-tab-pane>
      <el-tab-pane label="零工计划" name="second">
        <BaseTable 
          ref="oddJobApply"
          :loading="lloading"
          :data="ltableData"
          @details="oddJobHandlerClick"
          :columns="lcolumns" 
          :tableHeight="tableHeight" 
          :pagination="false"></BaseTable>
      </el-tab-pane>
      <el-tab-pane label="集疏港计划" name="third">
        <BaseTable 
          ref="oddJobApply"
          :loading="jloading"
          :data="jtableData"
          @details="openPortHandlerClick"
          :columns="jcolumns" 
          :tableHeight="tableHeight" 
          :pagination="false"></BaseTable>
      </el-tab-pane>
      <el-tab-pane label="转运计划" name="four">
        <BaseTable 
          :loading="transshipmentloading"
          :data="transshipmentTableData"
          :columns="transshipmentColumns" 
          :tableHeight="tableHeight" 
          @details="transshipmenthandlerClick" 
          :pagination="false"></BaseTable>
      </el-tab-pane>
    </el-tabs>
    <!-- 指派理货员 -->
    <el-drawer title="理货员派工" :visible.sync="isShow" size="35%" destroy-on-close>
      <BaseTableCheck
        ref="steaftable"
        :loading="sloading"
        :data="sTableData"
        :columns="sColumns" 
        :tableHT="tableHt" 
        :pagination="false" 
        @checkId="steafselectCheck">
      </BaseTableCheck>
      <div class="demo-drawer__footer">
        <el-button
          size="small"
          type="primary"
          @click="saveTally()"
        >保 存</el-button>
      </div>
    </el-drawer>
    <!-- 指派检尺 -->
    <el-drawer title="检尺派工" :visible.sync="isShowMeasuring" size="35%">
      <BaseTableCheck
        ref="measuringtable"
        :loading="mloading"
        :data="mTableData"
        :columns="mColumns" 
        :tableHT="tableHt" 
        :pagination="false" 
        @checkId="measuringselectCheck">
      </BaseTableCheck>
      <div class="demo-drawer__footer">
        <el-button
          size="small"
          type="primary"
          @click="addUpdate()"
        >保 存</el-button>
      </div>
    </el-drawer>
    <!-- 场地安排 -->
    <el-dialog
      title="提示"
      :visible.sync="dialogVisible"
      width="30%">
      <el-form :model="form" label-width="100px">
        <el-form-item label="起始区域：">
          <el-select v-model="form.storehouseIdSource" multiple collapse-tags size="mini" clearable placeholder="请选择">
            <el-option
              v-for="item in yards"
              :key="item.id"
              :label="item.storehouseName"
              :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="终点区域：">
          <el-select v-model="form.storehouseIdTarget" multiple collapse-tags size="mini" clearable placeholder="请选择">
            <el-option
              v-for="item in yards"
              :key="item.id"
              :label="item.storehouseName"
              :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="submitYard">确 定</el-button>
      </span>
    </el-dialog>
    <!-- 指派外理单位 -->
    <el-dialog
      title="指派外理单位"
      :visible.sync="isShowExternalReaon"
      width="30%">
      <el-form :model="form" label-width="100px">
        <el-form-item label="外理单位：">
          <el-select 
            v-model="form.foreignTallyCompanyId" 
            size="mini" 
            clearable 
            placeholder="请选择"
            v-selecttext="form.foreignTallyCompanyName,externalList,'foreignTallyCompanyId'">
            <el-option
              v-for="item in externalList"
              :key="item.foreignTallyCompanyId"
              :label="item.foreignTallyCompanyName"
              :value="item.foreignTallyCompanyId">
            </el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="isShowExternalReaon = false">取 消</el-button>
        <el-button type="primary" @click="submitExternal">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
import publicapi from '@/api/public/index'
import BaseTable from '@/components/BaseTable'
import BaseTableCheck from '@/components/BaseTable/baseTablecheck'
import api from '../../../api/yardTally/houseYardDispatch'
export default {
  components: { BaseTable,BaseTableCheck },
  data() {
    return{
      searchForm: {
        workDate: '',
        workTime: '01',
      },
      workTimeList: [], // 班次
      activeName: 'first',
      loading: false,
      tableHeight: window.innerHeight - 220,
      tableHt: window.innerHeight - 130,
      tableData: [], // 船舶计划
      columns: [
        {
          prop: 'planNo',
          label: '序号',
        },
        {
          prop: 'processName',
          label: '作业过程',
        },
        {
          prop: 'nameVoyage',
          label: '船名航次',
        },
        {
          prop: 'cargoCategoryName',
          label: '货种',
        },
        {
          prop: 'customerName',
          label: '客户名称',
        },
        {
          prop: 'loadUnload',
          label: '装卸',
        },
        {
          prop: 'berthNo',
          label: '泊位',
        },
        {
          prop: 'storehouseName',
          label: '目的位置',
        },
        {
          prop: 'deptName',
          label: '作业工段',
        },
        {
          prop: 'cooperationName',
          label: '协力队',
        },
        {
          prop: 'tallyFront',
          label: '前沿',
        },
        {
          prop: 'transport',
          label: '转运',
        },
        {
          prop: 'tallyBack',
          label: '后场',
        },
        {
          prop: 'supporting',
          label: '辅助',
        },
        {
          prop: 'laborDispatch',
          label: '劳务',
        },
        {
          prop: 'foreignTallyCompanyName',
          label: '外理单位'
        },
        {
          prop: 'tallyClerk',
          label: '理货员',
        },
        {
          prop: 'ruler',
          label: '检尺员',
        },
        {
          prop: 'instructorName',
          label: '指导员',
          width: 130
        },
        {
          prop: 'workloadPlan',
          label: '计划量',
        },
        {
          prop: 'endTimePlan',
          label: '计划完工时间',
        },
        {
          prop: 'remark',
          label: '作业要求',
        },
      ],
      lloading: false,
      ltableData: [], // 零工申请
      lcolumns: [
        {
          prop: 'planNo',
          label: '编号',
          width: 120
        },
        {
          prop: 'approvalStatus',
          label: '状态',
          render(h,params) {
            if (params.row.approvalStatus == '1') {
              return h('el-tag', {
                props: {
                  size: 'mini'
                },
                domProps: {
                  innerHTML: '未审核'
                }
              })
            } else if (params.row.approvalStatus == '2') {
              return h('el-tag', {
                props: {
                  size: 'mini',
                  type: 'success'
                },
                domProps: {
                  innerHTML: '已审核'
                }
              })
            }
          }
        },
        {
          prop: 'processName',
          label: '作业过程',
        },
        {
          prop: 'nameVoyage',
          label: '船名航次',
          width: 120
        },
        {
          prop: 'cargoCategoryName',
          label: '货种',
        },
        {
          prop: 'workContent',
          label: '作业内容',
        },
        {
          prop: 'deptName',
          label: '作业工段',
        },
        {
          prop: 'supporting',
          label: '机械',
          width: 120,
        },
        {
          prop: 'laborDispatch',
          label: '劳务',
        },
        {
          prop: 'applyDeptName',
          label: '申请单位',
        },
        {
          prop: 'foreignTallyCompanyName',
          label: '外理单位'
        },
        {
          prop: 'tallyClerk',
          label: '理货员',
        },
        {
          prop: 'ruler',
          label: '检尺员',
        },
        {
          prop: 'instructorName',
          label: '指导员',
          width: 130
        },
        {
          prop: 'remark',
          label: '备注',
        },
      ],
      jloading: false,
      jtableData: [], // 集疏港
      jcolumns: [
        {
          prop: 'planNo',
          label: '序号',
        },
        {
          prop: 'processName',
          label: '作业过程',
        },
        {
          prop: 'nameVoyage',
          label: '船名航次',
        },
        {
          prop: 'cargoCategoryName',
          label: '货种',
        },
        {
          prop: 'customerName',
          label: '客户名称',
        },
        {
          prop: 'loadUnload',
          label: '装卸',
        },
        {
          prop: 'berthNo',
          label: '泊位',
        },
        {
          prop: 'storehouseName',
          label: '目的位置',
        },
        {
          prop: 'deptName',
          label: '作业工段',
        },
        {
          prop: 'cooperationName',
          label: '协力队',
        },
        {
          prop: 'tallyFront',
          label: '前沿',
        },
        {
          prop: 'transport',
          label: '转运',
        },
        {
          prop: 'tallyBack',
          label: '后场',
        },
        {
          prop: 'supporting',
          label: '辅助',
        },
        {
          prop: 'laborDispatch',
          label: '劳务',
        },
        {
          prop: 'foreignTallyCompanyName',
          label: '外理单位'
        },
        {
          prop: 'tallyClerk',
          label: '理货员',
          width: 120
        },
        {
          prop: 'ruler',
          label: '检尺员',
          width: 120
        },
        {
          prop: 'instructorName',
          label: '指导员',
          width: 130
        },
        {
          prop: 'workloadPlan',
          label: '计划量',
        },
        {
          prop: 'endTimePlan',
          label: '计划完工时间',
        },
        {
          prop: 'remark',
          label: '作业要求',
        },
      ],
      isShow: false,
      sloading: false,
      sTableData: [], // 理货员
      sColumns: [
        {
          prop: 'userNo',
          label: '工号'
        },
        {
          prop: 'operatorsName',
          label: '姓名'
        },
        {
          prop: 'deptName',
          label: '部门'
        },
        {
          prop: '',
          label: '位置',
          render: (h,params) => {
            return h('el-select', {
              props: {
                value: params.row.position,
                size: 'mini',
                filterable:true,
                clearable:true,
              },
              on: {
                'change': e => {
                  console.log(e)
                  this.$set(this.sTableData[params.row.index],'position',e)
                },
              }
            }, this.positionList.map((item) => {
              return h('el-option', {
                props: {
                  value: item.value,
                  label: item.label,
                  size: 'mini'
                }
              })
            }))
          }
        },
      ],
      steafData: [], // 理货员选中的数据 
      isShowMeasuring: false,
      mloading: false,
      mTableData: [],
      mColumns: [
        {
          prop: 'userNo',
          label: '工号'
        },
        {
          prop: 'operatorsName',
          label: '姓名'
        },
        {
          prop: 'deptName',
          label: '队别'
        },
        {
          prop: 'duty',
          label: '职责',
          render: (h,params) => {
            return h('el-select', {
              props: {
                value: params.row.duty,
                size: 'mini',
                filterable:true,
                clearable:true,
              },
              on: {
                'change': e => {
                  console.log(e)
                  this.$set(this.mTableData[params.row.index],'duty',e)
                },
              }
            }, this.responsibilityList.map((item) => {
              return h('el-option', {
                props: {
                  value: item.value,
                  label: item.label,
                  size: 'mini'
                }
              })
            }))
          }
        },
      ],
      measuringData: [], // 选中的检尺数据
      responsibilityList: [
        {
          value: '1',
          label: '检尺',
        },
        {
          value: '2',
          label: '抄码',
        },
      ],
      positionList: [
        {
          value: '1',
          label: '前沿',
        },
        {
          value: '2',
          label: '转运',
        },
        {
          value: '3',
          label: '后场',
        },
        {
          value: '6',
          label: '辅助',
        },
      ],
      selectShipInfo: null, // 选中的船舶计划行
      selectOddJobInfo: null, // 选中的零工计划行
      selectOpenPortInfo: null, // 选中的集疏港计划行
      planId: '', // 计划id
      dialogVisible: false,
      yards: [], // 库场
      form: {
        storehouseIdSource: [],
        storehouseIdTarget: [],
        foreignTallyCompanyId: '',
      },
      isShowExternalReaon: false,
      externalList: [], // 外理单位
      transshipmentloading: false,
      transshipmentTableData: [], // 转运
      transshipmentColumns: [
        {
          prop: 'planNo',
          label: '编号',
          width: 140
        },
        {
          prop: 'processName',
          label: '作业过程'
        },
        {
          prop: 'nameVoyage',
          label: '船名航次',
          width: 120
        },
        {
          prop: 'cargoCategoryName',
          label: '货种'
        },
        {
          prop: 'customerName',
          label: '货代',
          isShort: true,
        },
        {
          prop: 'loadUnload',
          label: '装卸'
        },
        {
          prop: 'berthNo',
          label: '泊位'
        },
        {
          prop: 'storehouseNameSource',
          label: '起始区域'
        },
        {
          prop: 'storehouseNameTarget',
          label: '终点区域'
        },
        {
          prop: 'deptName',
          label: '作业工段',
          width: 130
        },
        {
          prop: 'cooperationName',
          label: '协力队',
          width: 130
        },
        {
          prop: 'tallyFront',
          label: '前沿',
          width: 130
        },
        {
          prop: 'transport',
          label: '转运',
          width: 130
        },
        {
          prop: 'tallyBack',
          label: '后场',
          width: 130
        },
        {
          prop: 'supporting',
          label: '辅助',
          width: 130
        },
        {
          prop: 'laborDispatch',
          label: '劳务',
          width: 130
        },
        {
          prop: 'foreignTallyCompanyName',
          label: '外理单位'
        },
        {
          prop: 'tallyClerk',
          label: '理货员',
          width: 130
        },
        {
          prop: 'ruler',
          label: '检尺员',
          width: 130
        },
        {
          prop: 'instructorName',
          label: '指导员',
          width: 130
        },
        {
          prop: 'workloadPlan',
          label: '计划量'
        },
        {
          prop: 'endTimePlan',
          label: '计划完工时间',
          width: 120
        },
        {
          prop: 'remark',
          label: '作业要求'
        },
      ],
      transshipmentInfo: null,
    }
  },
  activated() {
    this.getDictData()
    this.getYardData()
    this.queryCustomer()
    //  日期默认当天
		let date = new Date()
		let theYear = date.getFullYear() //获取完整的年份(4位)
		let theMonth = date.getMonth() + 1 //获取当前月份(1-12,1代表1月)
		theMonth = theMonth >= 10 ? theMonth : "0" + theMonth
		let theDay = date.getDate() //获取当前日(1-31)
		theDay = theDay >= 10 ? theDay : "0" + theDay
    this.searchForm.workDate = theYear+'-'+theMonth+'-'+theDay
  },
  methods: {
    // 查询字典信息
    getDictData () {
      publicapi.getDictData('SCHEDULE').then(res => {
        this.workTimeList = res.data.data.SCHEDULE
      })
    },
    // 查询库场信息
    getYardData () {
      publicapi.listStorehouse().then(res => {
        this.yards = res.data.data
      })
    },
    // 查询理货员
    queryTally () {
      api.queryWorkPeople().then(res => {
        this.sTableData = res.data.data
      })
    },
    // 查询检尺员
    queryPeople () {
      api.queryPeople().then(res => {
        this.mTableData = res.data.data
      })
    },
    // 查询主列表
    searchData () {
      let params = Object.assign({},this.searchForm)
      if (this.activeName == 'first') {
        params.planType = '1'
        this.loading = true
        api.queryByPlanType(params).then(res => {
          if (res.data.code != '0000') {
            return this.$message.error(res.data.msg)
          }
          this.loading = false
          this.tableData = res.data.data
          this.selectShipInfo = null
          this.planId = ''
        })
      } else if (this.activeName == 'second') {
        params.planType = '3'
        this.lloading = true
        api.queryByPlanType(params).then(res => {
          if (res.data.code != '0000') {
            return this.$message.error(res.data.msg)
          }
          this.lloading = false
          this.ltableData = res.data.data
          this.selectOddJobInfo = null
          this.planId = ''
        })
      } else if (this.activeName == 'third') {
        params.planType = '2'
        this.jloading = true
        api.queryByPlanType(params).then(res => {
          if (res.data.code != '0000') {
            return this.$message.error(res.data.msg)
          }
          this.jloading = false
          this.jtableData = res.data.data
          this.selectOpenPortInfo = null
          this.planId = ''
        })
      } else if (this.activeName == 'four') {
        params.planType = '4'
        this.transshipmentloading = true
        api.queryByPlanType(params).then(res => {
          if (res.data.code != '0000') {
            return this.$message.error(res.data.msg)
          }
          this.transshipmentloading = false
          this.transshipmentTableData = res.data.data
          this.transshipmentInfo = null
          this.planId = ''
        })
      }

    },
    // tab点击
    handleClick (val) {
      console.log(val)
      this.activeName = val
      this.searchData()
    },
    // 理货员选中复选框
    steafselectCheck (val) {
      this.steafData = val
    },
    // 选中的检尺数据
    measuringselectCheck (val) {
      this.measuringData = val
    },
    // 点击船舶计划行
    handlerClick (val) {
      console.log(val)
      this.planId = val.id
      this.selectShipInfo = val
    },
    // 零工计划点击行
    oddJobHandlerClick (val) {
      console.log(val)
      this.planId = val.id
      this.selectOddJobInfo = val
    },
    // 集疏港计划点击行
    openPortHandlerClick (val) {
      console.log(val)
      this.planId = val.id
      this.selectOpenPortInfo = val
    },
    // 打开理货员派工抽屉
    showSteaf () {
      this.queryTally()
      console.log(this.activeName)
      if (this.activeName == "first") {
        if (this.selectShipInfo == null) {
          return this.$message.warning('请先选择一条数据')
        }
      } else if (this.activeName == "second") {
        if (this.selectOddJobInfo == null) {
          return this.$message.warning('请先选择一条数据')
        }
      } else if (this.activeName == "third") {
        if (this.selectOpenPortInfo == null) {
          return this.$message.warning('请先选择一条数据')
        }
      } else if (this.activeName == "four") {
        if (this.transshipmentInfo == null) {
          return this.$message.warning('请先选择一条数据')
        }
      }
      
      this.isShow = true
      if (this.$refs.steaftable)
        this.$refs.steaftable.clearSelection()
      let params = {
        planId: this.planId,
        dispatchType: '3'
      }
      api.queryById(params).then(res => {
        if (res.data.code != '0000') {
          return this.$message.error(res.data.msg)
        }
        if (res.data.data && res.data.data.length > 0) {
          res.data.data.forEach(v => {
            this.sTableData.forEach(i => {
              if (v.operatorsId == i.operatorsId) {
                i.isCheck = '1'
                i.position = v.position + ''
              }
            })
          })
          let checkedGuide = this.sTableData.filter(function (s) {
						return s.isCheck == "1"
					})
          //默认选中
					this.$nextTick(() => {
						checkedGuide.forEach(row => {
							this.$refs.steaftable.toggleRowSelection(row, true)
						})
					})
        }
        
      })
    },
    // 显示指派检尺抽屉
    showResponsibility () {
      if (this.activeName == "first") {
        if (this.selectShipInfo == null) {
          return this.$message.warning('请先选择一条数据')
        }
      } else if (this.activeName == "second") {
        if (this.selectOddJobInfo == null) {
          return this.$message.warning('请先选择一条数据')
        }
      } else if (this.activeName == "third") {
        if (this.selectOpenPortInfo == null) {
          return this.$message.warning('请先选择一条数据')
        }
      } else if (this.activeName == "four") {
        if (this.transshipmentInfo == null) {
          return this.$message.warning('请先选择一条数据')
        }
      }
      this.isShowMeasuring = true
      this.queryPeople()

      if (this.$refs.measuringtable)
        this.$refs.measuringtable.clearSelection()
      let params = {
        planId: this.planId,
        dispatchType: '4'
      }
      api.queryById(params).then(res => {
        if (res.data.code != '0000') {
          return this.$message.error(res.data.msg)
        }
        if (res.data.data && res.data.data.length > 0) {
          res.data.data.forEach(v => {
            this.mTableData.forEach(i => {
              if (v.operatorsId == i.operatorsId) {
                i.isCheck = '1'
                i.duty = v.duty + ''
              }
            })
          })
          let checkedGuide = this.mTableData.filter(function (s) {
						return s.isCheck == "1"
					})
          //默认选中
					this.$nextTick(() => {
						checkedGuide.forEach(row => {
							this.$refs.measuringtable.toggleRowSelection(row, true)
						})
					})
        }
        
      })
    },
    // 保存理货员
    saveTally () {
      let params = {
        planId: this.planId,
        list: this.steafData
      }
      api.insertTallyClerk(params).then(res => {
        if (res.data.code != '0000') {
          return this.$message.error(res.data.msg)
        }
        this.$message.success('保存成功')
        this.isShow = false
      })
    },
    // 指派指导员
    addUpdate () {
      let params = {
        planId: this.planId,
        list: this.measuringData
      }
      api.insertRuler(params).then(res => {
        if (res.data.code != '0000') {
          return this.$message.error(res.data.msg)
        }
        this.$message.success('保存成功')
        this.isShowMeasuring = false
      })
    },
    // 显示库场
    showYard () {
      if (this.activeName == "first") {
        if (this.selectShipInfo == null) {
          return this.$message.warning('请先选择一条数据')
        }
      } else if (this.activeName == "second") {
        if (this.selectOddJobInfo == null) {
          return this.$message.warning('请先选择一条数据')
        }
      } else if (this.activeName == "third") {
        if (this.selectOpenPortInfo == null) {
          return this.$message.warning('请先选择一条数据')
        }
      } else if (this.activeName == "four") {
        if (this.transshipmentInfo == null) {
          return this.$message.warning('请先选择一条数据')
        }
      }
      api.queryApplication(this.planId).then(res => {
        let storehouseIdSource = res.data.data.storehouseIdSource
        let storehouseIdTarget = res.data.data.storehouseIdTarget
        this.form.storehouseIdSource = storehouseIdSource ? storehouseIdSource.split(',') : []
        this.form.storehouseIdTarget = storehouseIdTarget ? storehouseIdTarget.split(',') : []
      })
      this.dialogVisible = true
    },
    // 确认场地
    submitYard () {
      if (this.form.storehouseIdSource.length == 0 && this.form.storehouseIdTarget.length == 0) {
        return this.$message.warning('请至少选择一个区域')
      }
      let storehouseNameSource = ''
      if (this.form.storehouseIdSource && this.form.storehouseIdSource.length > 0) {
        storehouseNameSource = this.form.storehouseIdSource.map(v => {
          let obj = this.yards.filter(i => i.id == v)[0].storehouseName
          return obj
        }).join(',')
      }
      let storehouseNameTarget = ''
      if (this.form.storehouseIdTarget && this.form.storehouseIdTarget.length > 0) {
        storehouseNameTarget = this.form.storehouseIdTarget.map(v => {
          let obj = this.yards.filter(i => i.id == v)[0].storehouseName
          return obj
        }).join(',')
      }
      let params = {
        id: this.planId,
        storehouseIdSource: this.form.storehouseIdSource.join(','),
        storehouseNameSource: storehouseNameSource,
        storehouseIdTarget: this.form.storehouseIdTarget.join(','),
        storehouseNameTarget: storehouseNameTarget,
      }
      console.log(params.storehouseIdSource)
      api.insertStorehouse(params).then(res => {
        if (res.data.code != '0000') {
          return this.$message.error(res.data.msg)
        }
        this.$message.success('保存成功')
        this.searchData()
        this.dialogVisible = false
      })
    },
    // 显示派外理单位
    showExternalReason () {
      if (this.activeName == "first") {
        if (this.selectShipInfo == null) {
          return this.$message.warning('请先选择一条数据')
        }
      } else if (this.activeName == "second") {
        if (this.selectOddJobInfo == null) {
          return this.$message.warning('请先选择一条数据')
        }
      } else if (this.activeName == "third") {
        if (this.selectOpenPortInfo == null) {
          return this.$message.warning('请先选择一条数据')
        }
      } else if (this.activeName == "four") {
        if (this.transshipmentInfo == null) {
          return this.$message.warning('请先选择一条数据')
        }
      }
      api.queryForeignTallyByPlanId(this.planId).then(res => {
        let foreignTallyCompanyId = res.data.data[0]?res.data.data[0].foreignTallyCompanyId:''
        this.form.foreignTallyCompanyId = foreignTallyCompanyId
      })
      this.isShowExternalReaon = true
    },
    // 查询外理单位
    queryCustomer () {
      publicapi.queryCustomer().then(res => {
        this.externalList = res.data.data
      })
    },
    // 保存外理单位
    submitExternal () {
      if (!this.form.foreignTallyCompanyId) {
        return this.$message.warning('请选择外理后再保存')
      }
      this.form.id = this.planId
      delete this.form.storehouseIdSource
      delete this.form.storehouseIdTarget

      api.insertForeignTallyCompany(this.form).then(res => {
        if (res.data.code != '0000') {
          return this.$message.error(res.data.msg)
        }
        this.$message.success('保存成功')
        this.isShowExternalReaon = false
        this.searchData()
      })
    },
    // 转运选中
    transshipmenthandlerClick (val) {
      this.planId = val.id
      this.transshipmentInfo = val
    },
  }
}
</script>
<style lang="less" scoped>
@import "../../../assets/searchfrom.less";
</style>